package com.cuckoo.mall.thus.menucart;

import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.bgn.baseframe.utils.BaseStringUtil;
import com.bgn.baseframe.utils.UiUtil;
import com.bgn.baseframe.utils.imageloader.ImageLoader;
import com.bgn.baseframe.view.edittext.BackEditText;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cuckoo.mall.R;
import com.cuckoo.mall.javabean.CartItems;
import com.cuckoo.mall.javabean.CartProductItem;
import com.cuckoo.mall.javabean.Goods;
import com.cuckoo.mall.javabean.Product;

import java.util.List;

/**
 * Created by Administrator on 2018/3/31.
 */

public class CartQiuckAdapter extends BaseQuickAdapter<CartProductItem, BaseViewHolder> {
    private boolean editMode = false;
    private ItemCheckChange callBack;

    public CartQiuckAdapter(@Nullable List<CartProductItem> data, ItemCheckChange itemCheckChange) {
        super(R.layout.list_item_cart, data);
        callBack = itemCheckChange;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final CartProductItem item) {
        if (item.getProduct_info() == null) {
            //服务器异常兼容
            item.setProduct_info(new Product());
        }
        helper.setText(R.id.tv_property, BaseStringUtil.getAtributeSting(item.getAttribute()));
        helper.setChecked(R.id.check_edit, item.isCheckd());
        helper.setText(R.id.tv_name, item.getProduct_info().getName());
        helper.setText(R.id.et_counts, item.getNumber() + "");
        helper.setText(R.id.tv_price, item.getProduct_info().getCurrency() + " " + BaseStringUtil.getPrioceFormatString(item.getProduct_info().getPrice()));
        ImageLoader.load(item.getProduct_info().getImage()).into((ImageView) helper.getView(R.id.iv_image));

        if (item.getProduct_info().getMarket_price() == 0) {
            helper.setText(R.id.tv_max_price, "");
        } else {
            helper.setText(R.id.tv_max_price, BaseStringUtil.addDeleteLineToText(item.getProduct_info().getCurrency() + " " + BaseStringUtil.getPrioceFormatString(item.getProduct_info().getMarket_price())));
            // helper.setText(R.id.tv_max_price, Html.fromHtml("<strike>" + item.getProduct_info().getCurrency()+" " + BaseStringUtil.getPrioceFormatString(item.getProduct_info().getMarket_price()) + "</strike>"));
        }

        helper.addOnClickListener(R.id.iv_add);
        helper.addOnClickListener(R.id.et_counts);
        helper.addOnClickListener(R.id.iv_minus);
        ((CheckBox) helper.getView(R.id.check_edit)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setCheckd(isChecked);
                if (callBack != null) callBack.whenItemCheckChangeCllBack();
            }
        });
    }

    public void changeMode(boolean isEdit) {
        if (editMode != isEdit) {
            editMode = isEdit;
            notifyDataSetChanged();
        }
    }

    public interface ItemCheckChange {
        void whenItemCheckChangeCllBack();
    }
}
